package com.guigusuqi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigusuqi.app.entity.Doctor;
import com.guigusuqi.app.entity.TimeManagement;
import com.guigusuqi.app.entity.VO.TimeList;
import com.guigusuqi.app.entity.VO.TimeManagementVo;
import com.guigusuqi.app.mapper.DoctorMapper;
import com.guigusuqi.app.mapper.TimeManagementMapper;
import com.guigusuqi.app.service.TimeManagementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 时间安排管理 服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-05
 */
@Service
public class TimeManagementServiceImpl extends ServiceImpl<TimeManagementMapper, TimeManagement> implements TimeManagementService
{

    @Resource
    private DoctorMapper doctorMapper;

    @Override
    public List<TimeManagementVo> listAll()
    {
        QueryWrapper<TimeManagement> wrapperName = new QueryWrapper<>();
        QueryWrapper<TimeManagement> wrapperId = new QueryWrapper<>();
        wrapperId.select("DISTINCT doctor_id");
        List<TimeManagement> docterids = baseMapper.selectList(wrapperId);

        List<TimeManagementVo> timeManagementVos = new ArrayList<>();

        for (TimeManagement docid: docterids) {

            Integer doctorId = docid.getDoctorId();
            QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
            doctorQueryWrapper.select("name","intro","address","mobile","office_name","id");
            doctorQueryWrapper.eq("id",doctorId);
            Doctor doctor = doctorMapper.selectOne(doctorQueryWrapper);
            List<TimeList> timeLists = new ArrayList<>();
            TimeManagementVo timeManagementVo = new TimeManagementVo();
            timeManagementVo.setDoctorId(doctor.getId());
            timeManagementVo.setDoctorName(doctor.getName());
            timeManagementVo.setInfo(doctor.getIntro());
            timeManagementVo.setAddress(doctor.getAddress());
            timeManagementVo.setMobile(doctor.getMobile());
            timeManagementVo.setOfficeName(doctor.getOfficeName());

            QueryWrapper<TimeManagement> timeListWrapper = new QueryWrapper<>();
            timeListWrapper.eq("doctor_id",docid.getDoctorId());
            List<TimeManagement> timeManagements = baseMapper.selectList(timeListWrapper);

            for (TimeManagement times: timeManagements) {

                TimeList timeList = new TimeList();
                BeanUtils.copyProperties(times,timeList);
                timeList.setTimeid(times.getId());
                timeLists.add(timeList);
            }

            timeManagementVo.setTimeList(timeLists);
            timeManagementVos.add(timeManagementVo);
        }
        return timeManagementVos;
    }

    @Override
    public List<TimeManagementVo> conditionList(String time, String officeName) {
        QueryWrapper<TimeManagement> wrapperName = new QueryWrapper<>();
        QueryWrapper<TimeManagement> wrapperId = new QueryWrapper<>();
        if (!StringUtils.isEmpty(officeName)){
            wrapperId.select("DISTINCT doctor_id").eq("office_name",officeName);

        }else {
            wrapperId.select("DISTINCT doctor_id");
        }
        List<TimeManagement> docterids = baseMapper.selectList(wrapperId);

        List<TimeManagementVo> timeManagementVos = new ArrayList<>();
        for (TimeManagement docid: docterids) {
            Integer doctorId = docid.getDoctorId();
            QueryWrapper<Doctor> doctorQueryWrapper = new QueryWrapper<>();
            doctorQueryWrapper.select("name","intro","address","mobile","office_name","id");
            doctorQueryWrapper.eq("id",doctorId);
            Doctor doctor = doctorMapper.selectOne(doctorQueryWrapper);
            List<TimeList> timeLists = new ArrayList<>();
            TimeManagementVo timeManagementVo = new TimeManagementVo();
            timeManagementVo.setDoctorId(doctor.getId());
            timeManagementVo.setDoctorName(doctor.getName());
            timeManagementVo.setInfo(doctor.getIntro());
            timeManagementVo.setAddress(doctor.getAddress());
            timeManagementVo.setMobile(doctor.getMobile());
            timeManagementVo.setOfficeName(doctor.getOfficeName());
            QueryWrapper<TimeManagement> timeListWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(time)){
                timeListWrapper.eq("doctor_id",docid.getDoctorId()).eq("time",time);
            }else {
                timeListWrapper.eq("doctor_id",docid.getDoctorId());
            }

            List<TimeManagement> timeManagements = baseMapper.selectList(timeListWrapper);
            for (TimeManagement times: timeManagements) {
                TimeList timeList = new TimeList();
                BeanUtils.copyProperties(times,timeList);
                timeList.setTimeid(times.getId());
                timeLists.add(timeList);
            }
            timeManagementVo.setTimeList(timeLists);
            timeManagementVos.add(timeManagementVo);
        }
        return timeManagementVos;
    }
}
